package com.example.controller;

import com.example.bean.User;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@Api(tags = "用户相关的")
public class UserController {
    /**
     * 获取日志记录器
     */
    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${author}")
    private String name;

    @Autowired
    private UserService userService;



    @ApiIgnore
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String say() {
        return "hello world " + ", 我是 " + name;
    }

    @ApiOperation(value = "测试读取配置文件", notes = "test/read")
    @GetMapping(value = "test/read")
    public String speak() {
        return name + " 年龄是20";
    }

    @ApiOperation(value = "根据姓名查询用户", notes = "/query/name")
    @GetMapping(value = "/query/name")
    public User query(@RequestParam(value = "name", required = false, defaultValue = " ") String name) {
        return userService.findUserByName(name);

    }

    @ApiOperation(value = "查询所有的用户", notes = "/findAll")
    @CrossOrigin(origins = "*")
    @GetMapping(value = "/findAll")
    public List<User> findAll() {
        return userService.findAll();

    }

    @ApiOperation(value = "根据id删除个人信息", notes = "传入用户id")
    @DeleteMapping(value = "deleteuserbyId/{id}")
    public Boolean deleteUserById(@RequestParam(value = "id") String id) {
        Boolean b = userService.deleteUserById(id);
        return b;
    }

    @ApiOperation(value = "根据id修改个人信息", notes = "传入用户id")
    @PostMapping(value = "updateuser/byId")
    public Integer deleteUserById(@RequestBody User user) {
        Integer count = userService.updateUserById(user);
        return count;
    }

    @ApiOperation(value = "新增用户信息", notes = "新增不需要传入id")
    @PostMapping(value = "adduser")
    public Integer addUser(@RequestBody User user) {
        Integer count = userService.insertUser(user);
        return count;
    }

    @ApiOperation(value = "转账接口", notes = "转账接口模拟事务")
    @PutMapping(value = "changeMoney")
    public List<User> changeMoney() {
        userService.changeMoney();
        return userService.findAll();
    }



}

